package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.interceptor.LoginInterceptor;
import com.ruoyi.mapper.MyShortVideoMapper;
import com.ruoyi.model.LoginUser;
import com.ruoyi.model.MyShortVideo;
import com.ruoyi.request.MyShortVideoRequest;
import com.ruoyi.service.MyShortVideoService;
import com.ruoyi.vo.MyShortVideoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MyShortVideoImpl implements MyShortVideoService {

    @Autowired
    private MyShortVideoMapper myShortVideoMapper;

    @Override
    public Map<String, Object> selectMyShortVideoList(MyShortVideoRequest myShortVideoRequest) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();

        //封装分页信息
        Page<MyShortVideo> pageInfo = new Page<>(myShortVideoRequest.getPageIndex(), myShortVideoRequest.getPageNumber());


        IPage<MyShortVideo> recordDOIPage = myShortVideoMapper.selectPage(pageInfo, new QueryWrapper<MyShortVideo>()
                       .like("short_name", myShortVideoRequest.getShortName())
                       .eq("language", myShortVideoRequest.getLanguage())
                       .eq("is_hot", myShortVideoRequest.getIsHot())
                       .eq("is_new", myShortVideoRequest.getIsNew())
                       .orderByDesc("create_time"));

        log.info("recordDOIPage = {}", recordDOIPage);
        Map<String, Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", recordDOIPage.getTotal());
        pageMap.put("total_page", recordDOIPage.getPages());
        pageMap.put("current_data", recordDOIPage.getRecords().stream().map(obj -> beanProcess(obj)).collect(Collectors.toList()));

        return pageMap;
    }

    @Override
    public MyShortVideo findDetail(int videoId) {
        if (videoId > 0) {
            return myShortVideoMapper.selectById(videoId);
        }
        return null;
    }

    private MyShortVideo beanProcess(MyShortVideo MyShortVideo) {
        MyShortVideoVO myShortVideoVO = new MyShortVideoVO();
        BeanUtils.copyProperties(MyShortVideo,myShortVideoVO);
        return MyShortVideo;
    }
}
